//
//  TopicDetailsViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TopicDetailsViewController.h"
#import "TopicDetailsTableViewCell.h"
#import "TopicDetailsImageTableViewCell.h"
#import "Url_marco.h"
#import "GetDataHandle.h"
#import "TopicDetailsModel.h"
#import "ImageViewController.h"

#import <UIImageView+WebCache.h>
#import <MJRefresh.h>

#import <UMSocialData.h>
#import <UMSocialSnsService.h>

#import <UMSocialSnsPlatformManager.h>

//#import <umsocia>


@interface TopicDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *picurlImage;
@property (weak, nonatomic) IBOutlet UILabel *titleName;
@property (weak, nonatomic) IBOutlet UILabel *contentNameLabel;
@property (weak, nonatomic) IBOutlet UITableView *topicDetailsTableView;
@property (copy,nonatomic) NSMutableArray *dataArray;//评论数据源
@property (copy,nonatomic) NSMutableDictionary *dictDataDicrionary;//标题数据源
@property (copy,nonatomic) NSString *urlStr;
@property (copy,nonatomic) NSString *imageStr;//图片
@end

@implementation TopicDetailsViewController

- (void)viewWillAppear:(BOOL)animated{
    //    将导航栏设为透明
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    self.tabBarController.tabBar.hidden = YES;
}


//懒加载
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableDictionary *)dictDataDicrionary{
    if (!_dictDataDicrionary) {
        _dictDataDicrionary = [NSMutableDictionary dictionary];
    }
    return _dictDataDicrionary;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topicDetailsTableView.delegate = self;
    self.topicDetailsTableView.dataSource = self;
    
    self.topicDetailsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.topicDetailsTableView registerNib:[UINib nibWithNibName:@"TopicDetailsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    [self.topicDetailsTableView registerNib:[UINib nibWithNibName:@"TopicDetailsImageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"imageCell"];
    
 
    
    //    button弧度

    
//    上拉加载
    self.topicDetailsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        [self getSession];
    }];
    [self.topicDetailsTableView.mj_header beginRefreshing];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享"] style:UIBarButtonItemStyleDone target:self action:@selector(rightAC:)];
    
//    取消tableview 中的换行线
    self.topicDetailsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
}




//数据解析
- (void)getSession{
    self.urlStr = [NSString stringWithFormat:@"%@%@%@",TOPICDETAILS_URL,self.strID,POST_URL];
    
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:self.urlStr
withGetDataBlock:^(NSData *data) {
    NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *dic = di[@"data"];
    
    //将标题添加到标题数据源
    _dictDataDicrionary = dic[@"subject"];
    
    
 //    将评论数据添加到数据源
    NSArray *array1 = dic[@"latestList"];
    for (NSDictionary *dicti in array1) {
      
        TopicDetailsModel *model = [TopicDetailsModel new];
        [model setValuesForKeysWithDictionary:dicti];
        [self.dataArray addObject:model];
    }
    
   dispatch_async(dispatch_get_main_queue(), ^{
       [self.topicDetailsTableView.mj_header endRefreshing];
       [self.topicDetailsTableView reloadData];
   });
    
}];
    
    
}



- (void)loading{
    NSString *str = [NSString stringWithFormat:LODING];
    
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:str
                                         withGetDataBlock:^(NSData *data) {
    NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                                             NSDictionary *dic = di[@"data"];
                                             
//    将评论数据添加到数据源
    NSArray *array1 = dic[@"latestList"];
     for (NSDictionary *dicti in array1) {
                                                 
     TopicDetailsModel *model = [TopicDetailsModel new];
     [model setValuesForKeysWithDictionary:dicti];
     [self.dataArray addObject:model];
}
                                             
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.topicDetailsTableView.mj_footer endRefreshing];
        [self.topicDetailsTableView reloadData];
    });
                                             
}];


}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicDetailsModel *model = self.dataArray[indexPath.row];
    
    [self.picurlImage sd_setImageWithURL:[NSURL URLWithString:_dictDataDicrionary[@"picurl"]] placeholderImage:nil];
    self.titleName.text = _dictDataDicrionary[@"name"];
    self.contentNameLabel.text = _dictDataDicrionary[@"alias"];
    
    NSArray *array = model.picurl;
//    NSLog(@"%lu",(unsigned long)array.count);
   

    
    if (array.count != 0) {
        TopicDetailsImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
        cell.model = model;
        CGFloat height = [self textHeight:model.content];
        cell.contentLabel.frame = CGRectMake(cell.contentLabel.frame.origin.x, cell.contentLabel.frame.origin.y, cell.contentLabel.frame.size.width, height);
        
        [cell.imageButton addTarget:self action:@selector(imageButtonAC:) forControlEvents:(UIControlEventTouchUpInside)];
        NSArray *array = cell.array;
        NSLog(@"%@",array);
        self.imageStr = array.firstObject;
        return cell;
        

    }else{
        TopicDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.model = model;
        CGFloat height = [self textHeight:model.content];
        cell.content.frame = CGRectMake(cell.content.frame.origin.x, cell.content.frame.origin.y, cell.content.frame.size.width, height);
        
        return cell;
    }
    

    
    
}

- (void)imageButtonAC:(UIButton *)sender{
    
    ImageViewController *image = [ImageViewController new];
    image.imageStr = self.imageStr;
    [self showViewController:image sender:nil];

}

//计数字符串所占矩形区域的大小，并返回高度
- (CGFloat)textHeight:(NSString *)test{
    //    计算字符串所占的大小
    CGRect rect = [test boundingRectWithSize:CGSizeMake(394, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicDetailsModel *model = self.dataArray[indexPath.row];
    CGFloat height = [self textHeight:model.content];
    NSArray *array = model.picurl;
    if (array.count != 0) {
        return height + 200;
    }else{
        return height + 120;
    }
}

//分享按钮
- (void)rightAC:(UIBarButtonItem *)sender{
    NSString *str = [NSString stringWithFormat:@"主题：%@ http://c.3g.163.com/nc/qa/newsapp/topic.html?id= %@",self.dictDataDicrionary[@"name"],self.strID];
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:_dictDataDicrionary[@"picurl"]];
//    [UMSocialData defaultData].extConfig.title = @"分享的title";
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = self.urlStr;
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"57b3dcf367e58e7ccf002006"
                                      shareText:str
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToDouban,UMShareToQzone,UMShareToEmail,UMShareToSms,UMShareToQQ]
                                       delegate:self];

}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
